package com.af.community.smart.business

import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.af.community.smart.base.*
import com.af.community.smart.model.CommonData
import com.af.community.smart.utils.*
import com.af.community.smart.viewmodel.SignViewModel
import kotlinx.android.synthetic.main.layout_empty.*
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import org.jetbrains.anko.include
import org.koin.androidx.viewmodel.ext.android.getViewModel

class SignListActivity : BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include<View>(R.layout.layout_list)
        initTitle(title = "历史记录")

        swipe_refresh.isRefreshing = true
        getData(pageNum)
    }

    override fun getViewModel() = getViewModel<SignViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        empty_hint.text = "暂无相关签到信息！"
        swipe_refresh.refresh { getData(1) }
        recycle_list.apply {
            loadLinear(baseContext, swipe_refresh) {
                if (!isLoadingMore) {
                    isLoadingMore = true
                    getData(pageNum)
                }
            }

            addItemDecoration(object : NormalDecoration() {
                override fun getHeaderName(pos: Int): String = (mList[pos] as CommonData).letter
            }.apply {
                setHeaderContentColor(Color.parseColor("#F8F8F8"))
                setHeaderHeight(dp2px(50f))
                setTextSize(sp2px(14f))
                setTextColor(getColorEx(R.color.gray))
                setTextPaddingLeft(dp2px(15f))
            })
        }

        mAdapter = SlimAdapter.create()
            .register<CommonData>(R.layout.item_sign_list) { data, injector ->

                val index = mList.indexOf(data)
                val isLast = index == mList.size - 1

                injector.text(R.id.item_sign_time, "时间：${data.signTime.toFormatString()}")
                    .text(R.id.item_sign_addr, data.signAddress)
                    .visibility(
                        R.id.item_sign_divider,
                        if (isLast
                            || (!isLast && data.letter != (mList[index + 1] as CommonData).letter)) View.GONE
                        else View.VISIBLE
                    )
            }
            .attachTo(recycle_list)
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().getSignList(index, getString("userId"))
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        if (index == 1) {
                            clear()
                            pageNum = index
                        }
                        addItems(it)
                        if (count(it) > 0) pageNum++
                    }

                    mList.forEach {
                        it as CommonData
                        it.letter = it.signTime.toFormatString().toFormat(eFormat = "yyyy年M月")
                    }

                    mAdapter.updateData(mList)
                },
                onFinally = {
                    swipe_refresh.isRefreshing = false
                    isLoadingMore = false
                    empty_view.apply { if (mList.isEmpty()) visible() else gone() }
                }
            )
    }

}
